package cn.tedu.csmall.product.service.impl;

import cn.tedu.csmall.product.ex.ServiceException;
import cn.tedu.csmall.product.mapper.CategoryMapper;
import cn.tedu.csmall.product.pojo.dto.CategoryDTO;
import cn.tedu.csmall.product.pojo.dto.CategoryUpdateDTO;
import cn.tedu.csmall.product.pojo.entity.Album;
import cn.tedu.csmall.product.pojo.entity.Category;
import cn.tedu.csmall.product.pojo.vo.AlbumStandardVO;
import cn.tedu.csmall.product.pojo.vo.CategoryListVO;
import cn.tedu.csmall.product.pojo.vo.CategoryStandardVO;
import cn.tedu.csmall.product.service.ICategoryService;
import cn.tedu.csmall.product.web.ServiceCode;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * ClassName: CategoryServiceImpl
 * Package: cn.tedu.csmall.product.service.impl
 * Description 处理 类别 的具体业务实现类
 *
 * @version 0.0.1
 * @Author: 大傻
 * @Create:2022/12/3
 */
@Service
@Slf4j
public class CategoryServiceImpl implements ICategoryService {
    @Autowired
    CategoryMapper categoryMapper;

    public void addNew(CategoryDTO categoryDTO){
        int count = categoryMapper.countByName(categoryDTO.getName());
        if (count>0){
            String message = "添加失败,尝试添加的类别名称已被使用!";
            throw new ServiceException(ServiceCode.ERR_CONFLICT,message);
        }
        Category category = new Category();
        BeanUtils.copyProperties(categoryDTO,category);
        categoryMapper.insert(category);

    }

    @Override
    public void deleteById(Long id) {
        log.debug("正在处理根据id删除类别,参数{}",id);
        CategoryStandardVO categoryStandardVO = categoryMapper.selectById(id);
        if (categoryStandardVO==null){
            String massage = "删除类别失败,尝试删除的类别不存在!";
            log.error(massage);
            throw new ServiceException(ServiceCode.ERR_NOT_FOUND,massage);
        }
        log.debug("接下来即将删除数据,{}",id);
         categoryMapper.deleteById(id);
    }

    @Override
    public void setEnable(Long id) {
            updateEnableById(id,1);
    }

    @Override
    public void setDisable(Long id) {
        updateEnableById(id,0);
    }

    @Override
    public void setEnableDisplay(Long id) {
        updateEnableIsDisPlayById(id,1);
    }

    @Override
    public void setDisableDisplay(Long id) {
        updateEnableIsDisPlayById(id,0);
    }

    /**
     * 修改类别详情
     * @param id
     * @param categoryUpdateDTO
     */
    @Override
    public void updateInfoById(Long id, CategoryUpdateDTO categoryUpdateDTO) {
        log.debug("正在处理修改详情,参数id{}",id);
        CategoryStandardVO categoryStandardVO = categoryMapper.selectById(id);
        if (categoryStandardVO==null){
            String massage = "修改类别失败,尝试修改的数据不存在!";
            log.warn(massage);
            throw new ServiceException(ServiceCode.ERR_NOT_FOUND,massage);
        }
        //检查数据名称是否冲突
        int count = categoryMapper.countByNameAndNotId(id, categoryUpdateDTO.getName());
        if (count>0){
            String message = "修改类别基本信息失败,尝试修改的类别名称被占用";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERR_CONFLICT,message);
        }
      Category category = new Category();
        BeanUtils.copyProperties(categoryUpdateDTO,category);
        category.setId(id);
        log.debug("即将修改数据:{}",category);
        categoryMapper.update(category);
    }

    @Override
    public CategoryStandardVO getStandardById(Long id) {
        log.debug("开始处理根据id查询相册的业务,参数{}",id);
        CategoryStandardVO queryResult = categoryMapper.selectById(id);
        if (queryResult==null){
            String message="根基id查询类别详情失败,尝试访问的数据不存在!";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERR_NOT_FOUND,message);
        }

        return queryResult;
    }


    /**
     *  判断导航栏是否启用
     * @param id
     * @param isDisplay
     */
    public void updateEnableIsDisPlayById(Long id,Integer isDisplay){
        log.debug("正在处理判断是否显示导航栏");
        String [] enableText = {"禁用","启用"};
        CategoryStandardVO categoryStandardVO = categoryMapper.selectById(id);
        if (categoryStandardVO==null){
            String massage = enableText[isDisplay]+"失败,尝试修改的数据不存在!";
            log.warn(massage);
            throw new ServiceException(ServiceCode.ERR_NOT_FOUND,massage);
        }
        if (categoryStandardVO.getIsDisplay()==isDisplay){
            String massage = enableText[isDisplay]+"失败,尝试修改的数据已"+enableText[isDisplay]+"!";
            log.warn(massage);
            throw new ServiceException(ServiceCode.ERR_CONFLICT,massage);
        }
        Category category = new Category();
        category.setId(id);
        category.setIsDisplay(isDisplay);
        log.warn("即将修改数据,参数id:{}",id);
        categoryMapper.update(category);
    }

    /**
     * 判断是否启用
     * @param id
     * @param enable
     */
    public void updateEnableById(Long id,Integer enable){
        log.debug("正在处理判断是否开启");
        String [] enableText = {"禁用","启用"};
        CategoryStandardVO categoryStandardVO = categoryMapper.selectById(id);
        if (categoryStandardVO==null){
            String massage = enableText[enable]+"失败,尝试修改的数据不存在!";
            log.warn(massage);
            throw new ServiceException(ServiceCode.ERR_NOT_FOUND,massage);
        }
        if (categoryStandardVO.getEnable()==enable){
            String massage = enableText[enable]+"失败,尝试修改的数据已"+enableText[enable]+"!";
            log.warn(massage);
            throw new ServiceException(ServiceCode.ERR_CONFLICT,massage);
        }
        Category category = new Category();
        category.setId(id);
        category.setEnable(enable);
        log.warn("即将修改数据,参数id:{}",id);
        categoryMapper.update(category);

    }

    @Override
    public List<CategoryListVO> list() {
        log.debug("正在处理查询类别列表");
        List<CategoryListVO> list = categoryMapper.list();
        return list;
    }

}
